<?php
error_reporting(E_ALL ^ E_NOTICE); 

class mgmApps {
	/* LIVE SITE */
	/*private static $dbname = 'admin_mgmDB';
	private static $dbuser = 'admin_mgm';
	private static $dbpass = '{i12xs}';*/
	
	/* TEST SITE */
	/*private static $dbname = 'admin_mgm_testDB';
	private static $dbuser = 'admin_mgm';
	private static $dbpass = '{i12xs}';*/
	
	/* LOCAL SITE */
	/*private static $dbname = 'admin_mgm_testDB';
	private static $dbuser = 'root';
	private static $dbpass = '';*/
	
	public function DBconnect()	{
		mysql_connect('localhost',self::$dbuser,self::$dbpass);
		@mysql_select_db(self::$dbname) or die( "Unable to select database");
	}
	
	public function BASEurl()	{
		//echo "http://".$_SERVER['HTTP_HOST'].'/subscribeTest/'; //localhost
		echo "http://".$_SERVER['HTTP_HOST']."/"; //test server
		//echo SRCWEB_BASE.DS;
	}
	
	public function page_title() {
		if(empty($_SESSION['page'])) {
			$page_title = 'MGM - Members Get Members';
		} else {
			
			switch ($_SESSION['page']) {
				case "subscribe":
					$page_title = 'MGM - Subscribe Now';
				break;
				case "referral":
					$page_title = 'MGM - Referrals';
				break;
				case "success":
					$page_title = 'MGM - Success Submission';
				break;
				case "verify":
					$page_title = 'MGM - Verify Email';
				break;
				case "contact":
					$page_title = 'MGM - Contact Us';
				break;
				default:
					$page_title = 'MGM - Page Doesn\'t Exist';
			}
		}
		return $page_title;
	}
	
	public function gotoURL(){
		$selection = $_REQUEST['radiog_lite'];
		
		if ($selection == 1){
			$mgmID = $_REQUEST['sponsorTxtBox'];
			$dbconnect = new mgmApps();
			$dbconnect->DBconnect();
			$sql = "SELECT url_name FROM mgm_users WHERE user_MGM_id = '".$mgmID."' ";
			$query = mysql_query($sql);
			$row = mysql_fetch_array($query);
			$url_name = $row['url_name'];
			header("Location: ../$url_name");
		} else {
			header("Location: ../subscribe");
		}
	}
	
	public function setMember() {
		session_start();
		$dbconnect = new mgmApps();
		$dbconnect->DBconnect();
		
		if($_SESSION['referral'] != ''){
			$url_name = $_SESSION['referral'];
			$_SESSION['url_name'] = $_SESSION['referral'];
			$sql = "SELECT * FROM mgm_users WHERE url_name = '".$url_name."' ";
			$query = mysql_query($sql);
			while ($row = mysql_fetch_array($query)){
				$_SESSION['user_MGM_id'] = $row['user_MGM_id'];
				//$_SESSION['url_fullname'] = $row['fullname'];
			}
			//$row = mysql_fetch_array($query);
			//$test = $row['user_MGM_id'];
		} else {
			unset($_SESSION['url_name']);
			//unset($_SESSION['url_fullname']);
			$_SESSION['user_MGM_id'] = 'MGM000001';
		}
		//echo $user_MGM_id;
	}
	
	public function setPage() {
		session_start();
		$page = $_REQUEST['page'];
		
		if (empty($page)){
			unset($_SESSION['page']);
			unset($_SESSION['referral']);
			unset($_SESSION['url_name']);
			$_SESSION['user_MGM_id'] = 'MGM000001';
			//unset($_SESSION['user_MGM_id']);
		} else {
			if ($page == 'subscribe') {
				$_SESSION['page'] = 'subscribe';
				$_SESSION['referral'] = '';
			}else if ($page == 'success') {
				$_SESSION['page'] = 'success';
				//$_SESSION['referral'] = '';
			
			}else if ($page == 'verify') {
				$_SESSION['page'] = 'verify';
				//$_SESSION['referral'] = '';
			}else if ($page == 'contact') {
				$_SESSION['page'] = 'contact';
			} else {
				$_SESSION['page'] = 'referral';
				$_SESSION['referral'] = $page;
			}
		}
	}
	
	public function getPage() {
		$page = $_SESSION['page'];
		if(empty($page)){
			include 'templates/default/apps/home.php';
		} elseif ($page == 'subscribe' || $page == 'referral') {
			include 'templates/default/apps/subscribe.php';
		} elseif ($page == 'success') {
			include 'templates/default/apps/success.php';
		}elseif ($page == 'verify') {
			include 'templates/default/apps/verify.php';
		}elseif ($page == 'contact') {
			include 'templates/default/apps/contact.php';
		} else {
			include 'templates/default/apps/noexist.php';
		}
	}
	
	public function subscription() {
		$mgmApps = new mgmApps();
		$mgmApps->DBconnect();
		
		$result = mysql_query("SHOW TABLE STATUS LIKE 'mgm_users'");
		$row = mysql_fetch_array($result);
		$nextId = $row['Auto_increment'];   
		$usercode = "MGM".sprintf('%06d', $nextId-1);
		
		$sponsorID = htmlentities($_POST['sponsorID']);
		$email = htmlentities($_POST['email']);
		$urlname = htmlentities($_POST['urlname']);
		$password = htmlentities($_POST['password']);
		$fullname = htmlentities($_POST['fullname']);
		$ic_no = htmlentities($_POST['ic_no']);
		$state = htmlentities($_POST['state']);
		$country = htmlentities($_POST['country']);
		$address1 = htmlentities($_POST['address1']);
		$address2 = htmlentities($_POST['address2']);
		$postcode = htmlentities($_POST['postcode']);
		$phone_no = htmlentities($_POST['phone_no']);
		$mobile_no = htmlentities($_POST['mobile_no']);
		$verify_code = $mgmApps->rand_string(8);
		
		/*$sql = "SELECT * FROM mgm_subscribers WHERE id = '1'";
		$query = mysql_query($sql);
		$row = mysql_fetch_array($query);*/
		
		//echo $row['url_name'].' + '.$row['user_MGM_id'];
		//echo $usercode.' + '.$sponsorID.' + '.$email.' + '.$urlname.' + '.$password.' + '.$fullname.' + '.$ic_no.' + '.$state.' + '.$country.' + '.$address1.' + '.$address2.' + '.$postcode.' + '.$phone_no.' + '.$mobile_no.' + '.$verify_code;
		
		$sql = "INSERT INTO mgm_users (email, url_name, password, user_MGM_id, sponsor_id, fullname, identity_no, address1, address2,state, country, phone_no, mobile_no, verify_acc, submission_date) VALUES ('".$email."', '".$urlname."', '".$password."', '".$usercode."', '".$sponsorID."', '".$fullname."', '".$ic_no."', '".$address1."', '".$address2."', '".$state."', '".$country."', '".$phone_no."', '".$mobile_no."','".$verify_code."', NOW())";
		$query = mysql_query($sql);
		
		$sql2 = "SELECT id from mgm_users WHERE user_MGM_id = '".$usercode."'";
		$query2 = mysql_query($sql2);
		$row2 = mysql_fetch_array($query2);
		$usercode2 = $row2['id'];
		
		//echo $usercode2;
		
		$sql_date = "INSERT INTO user_subscription (user_id, start_month, end_month) VALUES ('".$usercode2."',NOW(),NOW() + interval 3 month)";
		$query_date = mysql_query($sql_date);
		
		if(!$query || !$query_date){
			die(mysql_error());
		} else {
			$mgmApps->sendEmail($usercode,$sponsorID);
			//header('Location: ../success');
		}
		
	}
	
	public function rand_string($length) {
		$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";	
		$size = strlen( $chars );
		for( $i = 0; $i < $length; $i++ ) {
			$str .= $chars[ rand( 0, $size - 1 ) ];
		}
	return $str;
	}
	
	public function sendEmail($usercode,$sponsorID) {
		include 'templates/default/apps/mail.php';
		include 'templates/default/apps/sponsorMail.php';
		header('Location: ../success');
	}
	
} // end class mgmApps

?>